const { default: DPagination } = require("../../../utils/DPagination");
const { getProjectNameLang } = require("../common");
var location = require("../../../mybehaviors/location/location");


// packageH/project_verification/MoreStores/MoreStores.js
const App = getApp();
const StorePagination = new DPagination("stores");
const GetProjectUrl = App.getNetAddresss("plugin.store-projects.frontend.project.get-detail");
const GetProjectDetailsUrl = App.getNetAddresss("plugin.store-projects.frontend.project-order.get-detail");
const GetStoreUrl = App.getNetAddresss("plugin.store-projects.frontend.store.get-list-by-project-id");
let Location = null;
Page({
  behaviors: [location],
  /**
   * 页面的初始数据
   */
  data: {
    projectId: null,
    pageFrom: "details",
    stores: [],
    project: null,
    projectSimpleMode: false,
    projectGoldenStyle: false,
    projectNameLang: getProjectNameLang(),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    if (!options.project_id) {
      wx.showToast({
        title: getProjectNameLang() + "不存在",
        icon: "none",
      });
      wx.navigateBack({
        delta: 1,
      });
      return;
    }
    StorePagination.bind(this);
    StorePagination.clean();
    const setData = {
      projectId: options.project_id,
    };
    if (options.from) {
      setData["pageFrom"] = options.from;
    }
    if (options.from === "details" || options.from === "goods") {
      setData["projectGoldenStyle"] = true;
      setData["projectSimpleMode"] = true;
    }

    this._getLocation((res,point) => {
      Location = {
        point: point
      };
      this.setData(setData);
      this.getProject();
    });   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    StorePagination.clean();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    StorePagination.clean();
    this.getProject();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getProjectStore();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  getProject() {
    let point = App.qqMapTransBMap(parseFloat(Location.point.lng), parseFloat(Location.point.lat));
    const requestParmas = {
      lng: point.lng,
      lat: point.lat,
    };
    let url = GetProjectUrl;
    if (this.data.projectSimpleMode) {
      requestParmas["project_id"] = this.data.projectId;
    } else {
      url = GetProjectDetailsUrl;
      requestParmas["id"] = this.data.projectId;
    }
    App._getNetWork({
      url,
      data: requestParmas,
      success: ({ data: { data: response }, result, msg }) => {
        if (result == 0) {
          wx.showToast({
            title: msg,
            icon: "none",
          });
          return;
        }
        if (this.data.projectSimpleMode) {
          this.setData({
            project: response.project,
          });
        } else {
          this.setData({
            project: response,
          });
        }

        this.getProjectStore();
      },
    });
  },
  getProjectStore() {
    if (StorePagination.loading || StorePagination.finished) {
      return;
    }
    StorePagination.loading = true;
    let point = App.qqMapTransBMap(parseFloat(Location.point.lng), parseFloat(Location.point.lat));
    App._getNetWork({
      url: GetStoreUrl,
      data: {
        project_id: this.data.project.project_id || this.data.project.id,
        lng: point.lng,
        lat: point.lat,
        page: StorePagination.loadPage,
      },
      success: ({ data: { data: response }, result, msg }) => {
        StorePagination.loading = false;
        if (result == 0) {
          wx.showToast({
            title: msg,
            icon: "none",
          });
          return;
        }
        if (response.current_page == response.last_page || response.data.length < response.per_page) {
          StorePagination.finished = true;
        }
        StorePagination.push(response.data);
        wx.stopPullDownRefresh();
      },
    });
  },
});
